<?php
$isReloadable = false;
require_once("../../../pers/inc.config.php");
require_once('../../_inc/inc.session.php');
require_once('../../_inc/funct.html.php');

$aCamForm = array();
require "_inc/const.campiMod.php";

$aVarExt['HtmlPopUpPagIni'] = "<a href=\"javascript:apriPopUpInsPag('id_pagina_ini');\">"
	."<img src=\"".OVF_URL_ROOT."/app/_img/popUp.png\" title=\"aggiungi una nuova pagina\" border=\"0\"></a>";
$aVarExt['HtmlPopUpPagVis'] = "<a href=\"javascript:apriPopUpInsPag('id_pagina_vis');\">"
	."<img src=\"".OVF_URL_ROOT."/app/_img/popUp.png\" title=\"aggiungi una nuova pagina\" border=\"0\"></a>";
	
if (reqVar('azione') == 'salva') {
	$msgPag = conferma($aCamForm);
} 

$sqlSel = "SELECT m.* FROM wfm_moduli m WHERE m.id_modulo = ".reqVarInt('id');
$objDB->exec($sqlSel);
if(!$aRow = $objDB->fetch()) {
	backOnErr("id. errato");
}
foreach($aCamForm as $rX => $aRX) {
	foreach($aRX as $jX => $aCX) {
		switch ($aCX[4]) {
			case 'date':
				reqDef($aCX[0],YMD2DMY($aRow[$aCX[0]]));
				break;
			case 'numero':
			case 'intero':
				reqDef($aCX[0],num2str($aRow[$aCX[0]]));
				break;
			default:
				reqDef($aCX[0],$aRow[$aCX[0]]);
				break;
		}			
	}
}
reqDef('cod_modulo_old',$aRow['cod_modulo']);
$aDefCampi = unserialize($aRow['def_campi']);

$sCmdR = "";
$sCmdR .= "<a class=\"cmd1\" href=\"javascript:document.frm1.submit();\" >salva</a>";
$sCmdR .= cmdTorna('annulla');

testaHtml('Gestione work-flow','Modifica modulo',$msgPag,'gest');
?>
<script language="JavaScript" type="text/javascript">
function apriPopUpInsPag(campo){
  	url = "pagInsPopUp.php?id=<?=reqvar('id');?>&xsn=<?=XSN?>&campo=" + campo;
	window.open(url, 'nuovaPag','width=730,height=300,top=50,left=50,resizable=yes,scrollbars=yes');
}
</script>
<table border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
   <tr>
      <td class="cmd1" align="right">
			<? echo $sCmdR; ?>
      </td>
   </tr>
</table>
<?=getHtmlForm('frm1','',array('enctype'=>true));?>
<?=getHtmlHidden('azione','salva');?>
<?=getHtmlHidden('id');?>
<?=getHtmlHidden('cod_modulo_old');?>
<?=getHtmlHidden('xsn',XSN);?>
<table class="tab1" border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
	<?=getHtmlRigaRif();?>
   <tr>
      <td class="tit1" colspan='20' align='center'>Modulo Work-Flow</td>
   </tr>
<?
echo getHtmlCampiForm($aCamForm,'inp',$aVarExt);
?>
   <tr>
      <td class="tit1" colspan='20' align='center'>Stati</td>
   </tr>
   <tr>
      <td class="lab1" colspan='2' align='center'>
      	&nbsp;
		</td>
      <td class="lab1" colspan='9' align='center'>
      	Stato
		</td>
      <td class="lab1" colspan='9' align='center'>
      	Note
		</td>
   </tr>
<?
$sqlSel = "SELECT s.* FROM wfm_stati s WHERE s.id_modulo = ".reqVarInt('id')
	." ORDER BY s.stato";
$objDB->exec($sqlSel);
$nn=0;
while($aRow = $objDB->fetch()) {
	reqDef("stato_$nn",$aRow['stato']);
	reqDef("stdes_$nn",$aRow['descr']);
	reqDef("stnot_$nn",$aRow['nota']);
	$nn++;
}
$nn+=3;
for($k=0;$k<$nn;$k++) {
?>
   <tr>
      <td class="txt1" colspan='2' align='center'>
      	<?=getHtmlTextNum("stato_$k",1,3)?>&nbsp;
		</td>
      <td class="txt1" colspan='9' align='center'>
      	<?=getHtmlText("stdes_$k",50,80)?>
		</td>
      <td class="txt1" colspan='9' align='center'>
      	<?=getHtmlText("stnot_$k",50)?>
		</td>
   </tr>
<?
}
?>
   <tr>
      <td class="tit1" colspan='20' align='center'>Campi</td>
   </tr>
   <tr>
      <td class="lab1" colspan='3' align='center'>
      	Nome
		</td>
      <td class="lab1" colspan='3' align='center'>
      	Etichetta
		</td>
      <td class="lab1" colspan='7' align='center'>
      	Tipo/lun/dec
		</td>
      <td class="lab1" colspan='3' align='center'>
      	dominio
		</td>
      <td class="lab1" colspan='3' align='center'>
      	default
		</td>
      <td class="lab1" colspan='1' align='center'>
      	p.
		</td>
   </tr>
<?
$numCampi=0;
foreach($aDefCampi as $sNome => $aDCX) {
	reqDef("dc_nom_$numCampi",$sNome);
	reqDef("dc_lab_$numCampi",$aDCX['lab']);
	reqDef("dc_tip_$numCampi",$aDCX['tip']);
	reqDef("dc_lun_$numCampi",$aDCX['lun']);
	reqDef("dc_dec_$numCampi",$aDCX['dec']);
	reqDef("dc_def_$numCampi",$aDCX['def']);
	reqDef("dc_dom_$numCampi",$aDCX['dom']);
	reqDef("dc_lis_$numCampi",$aDCX['lis']);
	$numCampi++;
}
$numCampi+=3;
if ($numCampi<10) $numCampi = 10;
require "../_inc/inc.aTipiCampo.php";
$aValTip = array(""=>"---");
foreach($aTipiCampo as $kX => $aX) {
	$aValTip[$kX] = $kX;
}
for($nn=0; $nn<$numCampi; $nn++) {
?>
   <tr>
      <td class="txt1" colspan='3' align='center'>
      	<?=getHtmlTextCode("dc_nom_$nn",10,20)?>
		</td>
      <td class="txt1" colspan='3' align='center'>
      	<?=getHtmlText("dc_lab_$nn",10,30)?>
		</td>
      <td class="txt1" colspan='7' align='center'>
      	<?=getHtmlSelect("dc_tip_$nn",$aValTip)?> (
      	<?=getHtmlTextNum("dc_lun_$nn",1,4)?>,
      	<?=getHtmlTextNum("dc_dec_$nn",1,2)?>)
		</td>
      <td class="txt1" colspan='3' align='center'>
      	<?=getHtmlText("dc_dom_$nn",20,100)?>
		</td>
      <td class="txt1" colspan='3' align='center'>
      	<?=getHtmlText("dc_def_$nn",20,100)?>
		</td>
      <td class="txt1" colspan='1' align='center'>
      	<?=getHtmlText("dc_lis_$nn",1,3)?>
		</td>
   </tr>
<?
}
?>
</table>
<table border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
   <tr>
      <td class="cmd1" align="right">
			<? echo $sCmdR; ?>
      </td>
   </tr>
</table>
<?
echo "</form>\n";
codaHtml();
exit();
function conferma($aCamForm) {
	global $objDB;
	$msg = '';
	$_REQUEST['cod_modulo'] = strtoupper(trim(reqVar('cod_modulo')));
	if (strlen(reqVar('cod_modulo'))< 4) {
		$msg .= "Codice obbligatorio di almeno 4 caratteri<br>";
	}
	if (preg_match("/[^A-Z0-9]/",reqVar('cod_modulo'))) {
		$msg = "Codice '".reqVar('cod_modulo')."' puo' contenere solo lettere numeri<br>";
	}
	if (trim(reqVar('descr'))=='') {
		$msg .= "Descrizione obbligatoria<br>";
	}
	if (reqVarStr2date('data_ini')===false) {
		$msg .= "Data inizio errata<br>";
	}
	if (reqVarStr2date('data_fin')===false) {
		$msg .= "Data fine errata<br>";
	}
	if (reqVar('id_pagina_ini')==0) {
		$msg .= "Selezionare pagina iniziale<br>";
	}
	if (reqVar('id_pagina_vis')==0) {
		$msg .= "Selezionare pagina visualizzazione<br>";
	}
	
	$sqlSel = "SELECT cod_modulo FROM wfm_moduli  " 
			." WHERE cod_modulo = '".reqVarAS('cod_modulo')."'"
			." AND id_modulo <> ".reqVarInt('id');
	$objDB->exec($sqlSel,'chkDup');
	$aRow = $objDB->fetch('chkDup');
	if ($aRow) {
		$msg .= "Codice gia' esistente<br>";
	}
	$aDefCampi = array();
	foreach($_REQUEST as $nX => $vX) {
		if (substr($nX,0,6)=='stato_' && $vX != '') {
			$msg .= checkStato(substr($nX,6));
		}
		if (substr($nX,0,7)=='dc_nom_' && $vX != '') {
			$nn = substr($nX,7);
			$msg .= checkDefCampo($nn);
			$sNome = reqVar("dc_nom_$nn");
			$aDefCampi[$sNome]['lab'] = reqVar("dc_lab_$nn");
			$aDefCampi[$sNome]['tip'] = reqVar("dc_tip_$nn");
			$aDefCampi[$sNome]['lun'] = reqVar("dc_lun_$nn");
			$aDefCampi[$sNome]['dec'] = reqVar("dc_dec_$nn");
			$aDefCampi[$sNome]['def'] = reqVar("dc_def_$nn");
			$aDefCampi[$sNome]['dom'] = reqVar("dc_dom_$nn");
			$aDefCampi[$sNome]['lis'] = reqVar("dc_lis_$nn");			
		}
	}
	if ($msg != '') {
		return $msg;
	}
	$sSql = "UPDATE wfm_moduli SET def_campi = '".addslashes(serialize($aDefCampi))."'";	
	foreach($aCamForm as $rX => $aRX) {
		foreach($aRX as $jX => $aCX) {
			switch ($aCX[4]) {
				case 'date':
					$sSql .= ", ".$aCX[0]." = '".reqVarStr2YMD($aCX[0])."' ";
					break;
				default:
					$sSql .= ", ".$aCX[0]." = '".reqVarAS($aCX[0])."' ";
					break;
			}			
		}
	}
	$sSql .= " WHERE id_modulo = ".reqVarInt('id');
	$objDB->exec($sSql,'updRec');
	//dbg_($sSql);
	$sSql = "DELETE FROM wfm_stati WHERE id_modulo = ".reqvarInt('id');
	$objDB->exec($sSql);
	foreach($_REQUEST as $nX => $vX) {
		if (substr($nX,0,6)!='stato_' || $vX == 0) continue;
		$nn = substr($nX,6);
		$sSql = "INSERT INTO wfm_stati SET id_modulo = ".reqvarInt('id')
			.", stato = ".reqVarInt("stato_$nn")
			.",descr = '".reqVarAs("stdes_$nn")."' "
			.",nota = '".reqVarAs("stnot_$nn")."' ";
		$objDB->exec($sSql);
	}
	header("Location: ".urlTorna(_t("modulo modificato")));
	exit();
}

function checkStato($nn) {
	global $objDB;
	$msg = '';
	if (preg_match("/[^0-9]/",reqVar("stato_$nn"))) {
		$msg .= "Stato ".($nn+1)." non numerico<br>";
		return $msg;
	}
	if (reqVarInt("stato_$nn")==0) {
		$msg .= "Stato ".($nn+1)." - zero non ammesso<br>";
		return $msg;
	}
	if (reqVarTrim("stdes_$nn")=='') {
		$msg .= "Stato ".($nn+1)." - manca descrizione<br>";
	}
	return $msg;
}


function checkDefCampo($nn) {
	global $objDB;
	$msg = '';
	$_REQUEST["dc_nom_$nn"] = strtolower(reqVarTrim("dc_nom_$nn"));
	if (preg_match("/[^_a-z0-9]/",reqVar("dc_nom_$nn"))) {
		$msg .= "campo ".($nn+1)." - nome non valido<br>";
	}
	if (reqVar("dc_tip_$nn")=='') {
		$msg .= "campo ".($nn+1)." - tipo non selezionato<br>";
	}
	if (reqVar("dc_lab_$nn")=='') {
		$msg .= "campo ".($nn+1)." - manca etichetta<br>";
	}
	switch (reqVar("dc_tip_$nn")) {
		case 'stringa':
			if (preg_match("/[^0-9]/",reqVar("dc_lun_$nn")) || reqVarInt("dc_lun_$nn")<=0) {
				$msg .= "campo ".($nn+1)." - lunghezza errata<br>";
			}
			break;
		case "numero" :
			if (preg_match("/[^0-9]/",reqVar("dc_lun_$nn")) || reqVarInt("dc_lun_$nn")<=0) {
				$msg .= "campo ".($nn+1)." - lunghezza errata<br>";
			}
			if (preg_match("/[^ 0-9]/",reqVar("dc_dec_$nn")) || reqVarInt("dc_dec_$nn")<0) {
				$msg .= "campo ".($nn+1)." - decimali errati<br>";
			}
			break;
		case 'tabella_glob':
		case 'tabella_spec':
		case 'lista':
			if (reqVarTrim("dc_dom_$nn")=="") {
				$msg .= "campo ".($nn+1)." - dominio errato errata<br>";
			}
			break;	
	}
	
	return $msg;
}
